layui.use(['table', 'admin','laydate','element', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var element = layui.element;
    var laydate = layui.laydate;


    laydate.render({
        elem: '#dispatchMonth'
        ,value:new Date()
        ,type:'month'
    });

    laydate.render({
        elem: '#dispatchMonth3'
        ,value:new Date()
        ,type:'month'
    });

    //tab切换
    element.on('tab(doTabBrief)',function(data){
        console.log(data.index)
    });

    /**
     * 客户订单体积匹配结果表管理
     */
    var TransportDispatchVolMatch = {
        tableId: "transportDispatchVolMatchTable"
    };

    /**
     * 初始化表格的列
     */
    TransportDispatchVolMatch.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '所属项目'},
            {field: 'dispatchDate', title: '发运日期'},
            {field: 'customerOrder', title: '客户单号'},
            {field: 'sysVol', sort: true, title: '系统体积'},
            {field: 'sysToovolume', sort: true, title: '系统已配体积'},
            {field: 'custVol', sort: true, title: '客户体积'},
            {field: 'volFlag', sort: true, title: '是否匹配成功'},
            {field: 'volFlagRemark', title: '匹配备注'},
            {field: 'replaceFlag', title: '是否已替换'},
            {field: 'replaceUser', title: '替换操作人'},
            {field: 'replaceTime', sort: true, title: '替换操作时间'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'settlementMonth', sort: true, title: '结算月份'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width:50, fixed: 'right'}
        ]];
    };

    /**
     * 导出excel按钮
     */
    TransportDispatchVolMatch.exportExcel = function () {
        var checkRows = table.checkStatus(TransportDispatchVolMatch.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['operateId'] = $("#operateId").val();
            queryData['customerOrder'] = $("#customerOrder").val();
            queryData['transportNumber'] = $("#transportNumber").val();
            queryData['volFlag'] = $("#volFlag").val();
            queryData['replaceFlag'] = $("#replaceFlag").val();
            queryData['dispatchMonth'] = $("#dispatchMonth").val();

            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/transportDispatchVolMatch/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };
    /**
     * 替换
     */
    TransportDispatchVolMatch.delAll = function () {
        var checkRows = table.checkStatus(TransportDispatchVolMatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要删除的数据");
            return ;
        } else {
            var ids = checkRows.data.map(function (item) {
                if(item.id != ''){
                    return item.id;
                }
            });
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/transportDispatchVolMatch/delAll", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(TransportDispatchVolMatch.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("确定要用客户体积替换系统体积吗?", operation);
        }

    };

    /**
     * 替换
     */
    TransportDispatchVolMatch.replaceOption = function () {
        var checkRows = table.checkStatus(TransportDispatchVolMatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要体积替换的数据");
            return ;
        } else {
            var ids = checkRows.data.map(function (item) {
                if(item.id != ''){
                    return item.id;
                }
            });

            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/transportDispatchVolMatch/replaceOption", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(TransportDispatchVolMatch.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("确定要用客户体积替换系统体积吗?", operation);
        }

    };
    /**
     * 点击查询按钮
     */
    TransportDispatchVolMatch.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['customerOrder'] = $("#customerOrder").val();
        queryData['transportNumber'] = $("#transportNumber").val();
        queryData['volFlag'] = $("#volFlag").val();
        queryData['replaceFlag'] = $("#replaceFlag").val();
        queryData['dispatchMonth'] = $("#dispatchMonth").val();
        table.reload(TransportDispatchVolMatch.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    TransportDispatchVolMatch.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/transportDispatchVolMatch/add';
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TransportDispatchVolMatch.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/transportDispatchVolMatch/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportDispatchVolMatch.onDeleteItem = function (data) {
        if(data.id == ''){
            Feng.error("请选择要删除的数据");
            return ;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportDispatchVolMatch/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportDispatchVolMatch.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '订单体积导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/transportDispatchVolMatch/toimportExcel',
            end: function () {
                table.reload(TransportDispatchVolMatch.tableId);
            }
        });
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportDispatchVolMatch.tableId,
        url: Feng.ctxPath + '/transportDispatchVolMatch/list',
        page: true,
        limits:[20,50,100,200,500,1000],
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportDispatchVolMatch.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportDispatchVolMatch.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TransportDispatchVolMatch.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TransportDispatchVolMatch.exportExcel();
    });

    // 替换体积
    $('#btnReplace').click(function () {
        TransportDispatchVolMatch.replaceOption();
    });

    $('#btnDelAll').click(function () {
        TransportDispatchVolMatch.delAll();
    });
    // 工具条点击事件
    table.on('tool(' + TransportDispatchVolMatch.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TransportDispatchVolMatch.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TransportDispatchVolMatch.onDeleteItem(data);
        }
    });

    /************************未匹配成功的客户表 start***************************/
    /**
     * 客户订单体积匹配结果表管理
     */
    var TransportDispatchVolMatch2 = {
        tableId: "transportDispatchVolMatchTable2"
    };

    /**
     * 初始化表格的列
     */
    TransportDispatchVolMatch2.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '所属项目'},
            {field: 'customerOrder', title: '客户单号'},
            {field: 'custVol', sort: true, title: '客户体积'},
            {field: 'settlementMonth', sort: true, title: '结算月份'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width:90, fixed: 'right'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportDispatchVolMatch2.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId2").val();
        queryData['customerOrder'] = $("#customerOrder2").val();
        queryData['settlementMonth'] = $("#settlementMonth2").val();
        table.reload(TransportDispatchVolMatch2.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 渲染表格
    var tableResult2 = table.render({
        elem: '#' + TransportDispatchVolMatch2.tableId,
        url: Feng.ctxPath + '/transportDispatchVolMatch/list2',
        page: true,
        limits:[20,50,100,200,500,1000],
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportDispatchVolMatch2.initColumn()
    });

    TransportDispatchVolMatch2.onDeleteItem = function (data) {
        if(data.id == ''){
            Feng.error("请选择要删除的数据");
            return ;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportDispatchVolMatch/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportDispatchVolMatch2.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        TransportDispatchVolMatch2.search();
    });

    // 工具条点击事件
    table.on('tool(' + TransportDispatchVolMatch2.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            TransportDispatchVolMatch2.onDeleteItem(data);
        }
    });
    /************************未匹配成功的客户表 end***************************/

    /************************未匹配成功的发运表 start***************************/
    /**
     * 客户订单体积匹配结果表管理
     */
    var TransportDispatchVolMatch3 = {
        tableId: "transportDispatchVolMatchTable3"
    };

    /**
     * 初始化表格的列
     */
    TransportDispatchVolMatch3.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'operateName', title: '所属项目'},
            {field: 'dispatchDate', title: '发运日期'},
            {field: 'customerOrder', title: '客户单号'},
            {field: 'sysVol', sort: true, title: '系统体积'},
            {field: 'sysToovolume', sort: true, title: '系统已配体积'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportDispatchVolMatch3.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId3").val();
        queryData['customerOrder'] = $("#customerOrder3").val();
        queryData['dispatchMonth'] = $("#dispatchMonth3").val();
        table.reload(TransportDispatchVolMatch3.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 渲染表格
    var tableResult3 = table.render({
        elem: '#' + TransportDispatchVolMatch3.tableId,
        url: Feng.ctxPath + '/transportDispatchVolMatch/list3',
        page: true,
        limits:[20,50,100,200,500,1000],
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportDispatchVolMatch3.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch3').click(function () {
        TransportDispatchVolMatch3.search();
    });
    /************************未匹配成功的发运表 end***************************/

});
